<template>
  <div>
    <!-- 面包屑导航 -->
    <div>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item><a href="/">商品管理</a></el-breadcrumb-item>
        <el-breadcrumb-item>商品分类</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-button type="primary" plain @click="addinfoGoods">添加分类</el-button>
    <tree-grid
      class="mt-15 mb-15"
      :treeStructure="true"
      :columns="columns"
      :data-source="dataSource"
      @deleteCate="deleteCategory"
      @editCate="editCategory"
    >
    </tree-grid>
    <!-- 分页 -->
     <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pagesizes"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalmun">
    </el-pagination>
    <!-- 添加分类弹出 -->
    <el-dialog title="添加角色" :visible.sync="addgoodsFormVisible">
        <el-form :model="addgoodsform" :rules="rules" status-icon ref="addgoodsform">
          <el-form-item label="分类名称" :label-width="formLabelWidth" prop="cat_name">
            <el-input v-model="addgoodsform.cat_name"></el-input>
          </el-form-item>
           <el-form-item label="父级名称" :label-width="formLabelWidth">
             <el-cascader
              placeholder="默认创建一级分类"
              change-on-select
              :options="options"
              :props="props"
              v-model="selectedOptions">
            </el-cascader>
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="addgoodsFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitForm('addgoodsform')">确 定</el-button>
        </div>
      </el-dialog>
  </div>
</template>

<script>
// 引入另外的 组件
import TreeGrid from '@/components/TreeGrid/TreeGrid'
import { goodsification, addgoodsInfo, categories } from '@/api/goodsApi'
export default {
  components: {
    TreeGrid
  },
  data () {
    return {
      dataSource: [], // 存放总数据的
      columns: [
        {
          text: '分类名称',
          dataIndex: 'cat_name',
          width: ''
        },
        {
          text: '是否有效',
          dataIndex: 'cat_deleted',
          width: ''
        },
        {
          text: '排序',
          dataIndex: 'cat_level',
          width: ''
        }
      ],
      totalmun: 0, // 总条数
      currentPage: 1, // 默认是第一页
      pagesizes: 5, // 默认的条数
      // 添加分类
      addgoodsFormVisible: false,
      formLabelWidth: '80px',
      addgoodsform: {
        cat_name: '',
        cat_level: '0',
        cat_pid: 0
      },
      props: {
        value: 'cat_id',
        label: 'cat_name'
      },
      options: [],
      selectedOptions: [],
      rules: {
        cat_name: [
          { required: true, message: '请输入角色名字', trigger: 'blur' }
        ]
      }
    }
  },
  mounted () {
    this.initList()
  },
  methods: {
    deleteCategory (cid) {
      console.log(cid)
    },
    editCategory (cid) {
      console.log(cid)
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.pagesizes = val
      this.initList()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.currentPage = val
      this.initList()
    },
    // 点击新增分类时事件
    addinfoGoods () {
      this.addgoodsFormVisible = true
      addgoodsInfo(2).then(res => {
        console.log(res)
        if (res.meta.status === 200) {
          this.options = res.data
        }
      })
    },
    // 数据初始化
    initList () {
      goodsification(3, {
        currentPage: this.currentPage,
        pagesize: this.pagesizes
      }).then(res => {
        // console.log(res)
        this.dataSource = res.data.result
        this.totalmun = res.data.total
      })
    },
    // 新加分类
    submitForm (formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // alert('submit!')
          let len = this.selectedOptions.length
          if (len > 0) {
            this.addgoodsform.cat_pid = len === 2 ? this.selectedOptions[1] : this.selectedOptions[0]
            this.addgoodsform.cat_level = len === 2 ? 2 : 1
          }
          categories(this.addgoodsform)
            .then(res => {
              console.log(res)
              if (res.meta.status === 201) {
                this.addgoodsFormVisible = false
                this.$message.success(res.meta.msg)
                this.addgoodsform = {
                  cat_name: '',
                  cat_level: '0',
                  cat_pid: 0
                }
                this.selectedOptions = []
                this.initList()
              } else {
                this.$message.success(res.meta.msg)
              }
            })
        } else {
          this.$message.error('错了哦，表单校验未通过')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
